<?php
/***************************************************************************
 *
 *   copyright            : (C) 2005 Winds of Storm
 *
 *   $Id: ignoring.php,v 1.8 2007/01/30 21:02:43 nathan Exp $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

$ignoring	= array('TYPE'		=>	'command',
			'NAME'		=>	'Ignoring User',
			'VERSION'	=>	'0.0.1',
			'CREDIT'	=>	'StormeRider',
			'FUNC'		=>	'pluginIgnoring',
			'HELP'		=>	'pluginIgnoringHelp');
$plugins[]	= $ignoring;

function pluginIgnoringHelp($help) {
	global	$ignoring;

	$help['ignore']		= array("/ignore [username]",
					"Add [username] to your ignore list. (Note: there is no need to use quotes with this command.)" .
					pluginCredit($ignoring));
	$help['unignore']	= array("/unignore [username]",
					"Remove [username] from your ignore list. (Note: there is no need to use quotes with this command.)" .
					pluginCredit($ignoring));
	$help['listignore']	= array('/listignore',
					'List all users you have on your ignore list.' .
					pluginCredit($ignoring));

	return $help;
}

function pluginIgnoring($userinfo, $event) {
	global	$db;
	global	$dbprefix;
	global	$msg;
	global	$p_row;

        $username       = $userinfo['username'];
        $userid         = $userinfo['userid'];
        $isMod          = $userinfo['isMod'];

	$msg			= $event[0]['msg'];

	if (eregi("^/ignore", $msg)) {
		$event[0]['parsed']	= 1;
		$event[0]['room']	= 0;
		$event[0]['userto']	= $userid;

		$random	= explode(" ", $msg, 2);
		$user	= $random[1];

		$uid	= getOnlineUserIdByName($user);
		if ($uid == -1) {
			$event[0]['msg']	= addslashes("<span class=sysmsg><b>*** ERROR: '$user' is not online to /ignore.</b></span>");
		} else {
			$sql	= "SELECT * FROM {$dbprefix}ignore WHERE ignoring_user_id=$userid AND ignored_user_id=$uid";
			$res	= $db->sql_query($sql);
			$rows	= $db->sql_numrows($res);

			if ($rows == 0) {
				$sql	= "INSERT INTO {$dbprefix}ignore (ignoring_user_id, ignored_user_id, ignored_user_name) " .
					  "VALUES ($userid, $uid, '$user')";
				$res	= $db->sql_query($sql);

				$event[0]['msg']	= addslashes("<span class=sysmsg><b>*** Adding '$user' to your ignore list.</b></span>");
				$event[0]['sflags']	= 1;
			} else {
				$event[0]['msg']	= addslashes("<span class=sysmsg><b>*** '$user' is already on your ignore list.</b></span>");
			}
		}
	} else if (eregi("^/unignore", $msg)) {
		$event[0]['parsed']	= 1;
		$event[0]['room']	= 0;
		$event[0]['userto']	= $userid;

		$random	= explode(" ", $msg, 2);
		$user	= $random[1];

		$sql	= "SELECT * FROM {$dbprefix}ignore WHERE ignoring_user_id=$userid AND ignored_user_name LIKE '$user'";
		$res	= $db->sql_query($sql);
		$rows	= $db->sql_numrows($res);

		if ($rows == 0) {
			$event[0]['msg']	= addslashes("<span class=sysmsg><b>*** Unable to locate '$user' on your ignore list. Please use /listignore to show the list of users you are ignoring.</b></span>");
		} else {
			$sql	= "DELETE FROM {$dbprefix}ignore WHERE ignoring_user_id=$userid AND ignored_user_name LIKE '$user'";
			$res	= $db->sql_query($sql);
			
			$event[0]['msg']	= addslashes("<span class=sysmsg><b>*** Removed '$user' from your ignore list.</b></span>");
			$event[0]['sflags']	= 1;
		}
	} else if (eregi("^/listignore", $msg)) {
		$event[0]['parsed']	= 1;
		$event[0]['room']	= 0;
		$event[0]['userto']	= $userid;

		$sql	= "SELECT * FROM {$dbprefix}ignore WHERE ignoring_user_id=$userid ORDER BY ignored_user_name";
		$res	= $db->sql_query($sql);
		
		while (($row = $db->sql_fetchrow($res))) {
			$igList[]	= $row['ignored_user_name'];
		}

		if (is_array($igList)) {
			$event[0]['msg']	= addslashes("<span class=sysmsg><b>*** You are currently ignoring: " . join(", ", $igList) . "</b></span>");
		} else {
			$event[0]['msg']	= addslashes("<span class=sysmsg><b>*** You have no one on your ignore list currently.</b></span>");
		}
	}
}
?>
